<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>tiles-server</title>
    <link rel="stylesheet" href="http://cdn.staticfile.org/openlayers/4.6.5/ol.css">
    <style>
        html,body {
            padding: 0;
            margin: 0 auto;
            width: 100%;
            height: 100%;
            min-width: 100%;
        }
        #map {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div id="map"></div>
    <script src="http://cdn.staticfile.org/openlayers/4.6.5/ol.js"></script>
    <script>
        // 地图设置中心
        var center = ol.proj.transform([114.02572999825531,22.64201285833866], 'EPSG:4326', 'EPSG:3857');

        // 创建地图
        var map = new ol.Map({
            view: new ol.View({
                center: center,
                zoom: 18 // 设置默认加载的级别
            }),
            target: 'map'
        });

        // 离线地图
        var offLineMap = new ol.layer.Tile({
            source: new ol.source.XYZ({
                tileUrlFunction: function(tileCoord) {
                    var z = tileCoord[0];
                    var x = tileCoord[1];
                    var y = -tileCoord[2];
                    y = (2**z-1)-y;
                    return 'http://localhost:8900/mbtiles/test/'+z+'/'+x+'/'+y+'.png';
                }
            })
        });

        map.addLayer(offLineMap);

        // 点击事件
        map.on('click', function(){
            // 获取可是区域中心点
            var mapExtent = map.getView().calculateExtent(map.getSize());
            var point = ol.extent.getCenter(mapExtent)
            point = ol.proj.transform([point[0], point[1]], 'EPSG:3857', 'EPSG:4326');
            alert(point);
        });
    </script>
</body>
</html>
